package com.abe.mvp.view.activity;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.ComboLineColumnChartData;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.ComboLineColumnChartView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;

import com.abe.config.PatternConstants;
import com.abe.mvp.view.activity.base.BaseActivity;
import com.abe.threadmill.R;
import com.abe.utils.ThreadPoolUtils;
import com.abe.utils.ToastUtils;

@ContentView(R.layout.activity_test)
public class TestActivity extends BaseActivity {
	private static final int APP_TOAST = 0x001;
	private static final int TWINKLE_CHART = 0x002;
	private static final int CHART_INIT = 0x003;

	private static final int SHOW_SELECT = 0x006;

	private static final int SET_SPEED = 0x007;
	private static final int SET_SLOPE = 0x008;
	@ViewInject(R.id.pattern_chart)
	private ComboLineColumnChartView pattern_chart;
	/** 原始数据 */
	private ComboLineColumnChartData current_data;
	/** 闪烁数据 */
	private ComboLineColumnChartData twinkle_data;

	/** 速度km/h */
	private List<Float> speeds;
	private List<Float> slopes;
	/** 记录手动设置速度数组 */
	private List<Float> change_speeds;
	private List<Float> change_slopes;

	/** 当前闪烁位置 （速度，坡度） */
	private int current_twinkle_pos = 0;
	/** 每组持续时间s */
	private int keep_group_time = 30;

	protected float current_slope = 0;
	/** m/s */
	protected float current_speed = 0;
	protected float old_speed, old_slope;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
	}

	private void initView() {
		/** 禁止手势缩放 */
		pattern_chart.setZoomEnabled(false);
		int pos = 0;
		/** 初始化原始速度 */
		speeds = PatternConstants
				.getListByArray(PatternConstants.model_speeds[pos]);
		slopes = PatternConstants
				.getListByArray(PatternConstants.model_slopes[pos]);
		/** 初始化速度记录数组 */
		change_speeds = speeds;
		change_slopes = slopes;
		current_data = PatternConstants.getComboLineColumnChartData(
				change_speeds, change_slopes);
		pattern_chart.setComboLineColumnChartData(current_data);
		current_twinkle_pos = 0;
		Viewport v = new Viewport(0, 20, speeds.size(), 0);
		pattern_chart.setCurrentViewport(v);
		ThreadPoolUtils.execute(new TwinkleChartThread());
	}

	/**
	 * @param status
	 *            false:隐藏，true：显示
	 * */
	public void setItemTwinkle(boolean status) {
		/** 控制数据与时间来实现闪烁状态 */
		List<Float> temSpeeds = new ArrayList<Float>();
		temSpeeds.addAll(change_speeds);
		if (status) {
			temSpeeds.set(current_twinkle_pos,
					change_speeds.get(current_twinkle_pos));
		} else {
			temSpeeds.set(current_twinkle_pos, 0f);
		}
		twinkle_data = PatternConstants.getComboLineColumnChartData(temSpeeds,
				change_slopes);
	}

	/**
	 * 闪烁线程
	 * */
	class TwinkleChartThread implements Runnable {
		@Override
		public void run() {
			while (current_twinkle_pos < speeds.size()) {
				for (int i = 0; i < keep_group_time / 0.5; i++) {
					try {
						/** 每隔500ms切换一下视图数据 */
						setItemTwinkle(i % 2 == 0);
						new MyHandler(Looper.getMainLooper())
								.sendEmptyMessage(TWINKLE_CHART);
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				/** 设置闪烁位置 */
				current_twinkle_pos++;
			}
		}
	}

	class MyHandler extends Handler {
		public MyHandler(Looper looper) {
			super(looper);
		}

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SET_SPEED:
				break;
			case SET_SLOPE:
				break;
			case APP_TOAST:
				ToastUtils.showShort(getApplicationContext(),
						msg.obj.toString());
				break;
			case TWINKLE_CHART: {
				/** 当前闪烁位置:current_twinkle_pos */
				pattern_chart.setComboLineColumnChartData(twinkle_data);
				pattern_chart.setVisibility(View.VISIBLE);
			}
				break;
			case CHART_INIT: {
				pattern_chart.setComboLineColumnChartData(current_data);
			}
				break;
			case SHOW_SELECT: {
				pattern_chart.setVisibility(View.VISIBLE);
			}
				break;
			default:
				break;
			}
		}

	}
}
